# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=cairo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_commit='b43e7c6f3cf7855e16170a06d3a9c7234c60ca94'
pkgver=1.17.6
pkgrel=3
pkgdesc="Cairo vector graphics library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://cairographics.org/"
license=(LGPL2.1 MPL1.1)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "git"
            )
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib"
        )
optdepends=("${MINGW_PACKAGE_PREFIX}-glib2: libcairo-gobject")
options=('strip' 'staticlibs')
source=("https://gitlab.freedesktop.org/cairo/cairo/-/archive/${_commit}/cairo-${_commit}.tar.gz"
        0026-create-argb-fonts.all.patch
        0030-ucrt-clang-fixes.patch
        302.patch
        0001-Fix-scaled_glyph-hash-lookup-on-Win64.patch)
sha256sums=('e65ca82c44308e36ff07ad860db4eb8b066223277a0f7c9440c839ff204ce86e'
            '6db6c44fbdb4926d09afa978fe80430186c4b7b7d255059602b1f94c6a079975'
            '86c1af2878a20bd3608fc476e4ba1f2451458a5f6f12e457e37b61082c38db82'
            '86401dee68cdd9463d5f520863d56caa336d06aead90a848e715ab64e11e469e'
            '51118597719e81c5b2a8a49626866c6c2356d5a56901747a848903e217d0ab6e')

prepare() {
  mv "${srcdir}/${_realname}-${_commit}" "${srcdir}/${_realname}-${pkgver}"
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/0026-create-argb-fonts.all.patch

  patch -p1 -i ${srcdir}/0030-ucrt-clang-fixes.patch

  # https://gitlab.freedesktop.org/cairo/cairo/-/merge_requests/302
  patch -p1 -i ${srcdir}/302.patch

  # https://gitlab.freedesktop.org/cairo/cairo/-/commit/1cc23206bde186d4aef948bba11e591120c99dcd
  patch -p1 -i ${srcdir}/0001-Fix-scaled_glyph-hash-lookup-on-Win64.patch
}

build() {
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix=${MINGW_PREFIX} \
    --buildtype=plain \
    --wrap-mode=nofallback \
    --default-library=both \
    -Dauto_features=enabled \
    -Dxlib=disabled \
    -Dxcb=disabled \
    -Dgl-backend=disabled \
    -Dtests=disabled \
    -Dspectre=disabled \
    -Dtee=enabled \
    -Dxml=enabled \
    -Dsymbol-lookup=disabled \
    ../${_realname}-${pkgver}

  meson compile
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" meson install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING"          "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING-LGPL-2.1" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-LGPL-2.1"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING-MPL-1.1"  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-MPL-1.1"
}
